<?php
class S3SDB {
	private $sdb;
	private $sdbDomainMasterList;
	private $sdbDomainVersionList;
	
	public $verbose = false;
	
	public function __construct($domainBase) {
		$this->sdb = new SDBUtils();
		$this->sdbDomainMasterList = $domainBase . "-masterList";
		$this->sdbDomainVersionList = $domainBase . "-versions";
	}
	
	public function notify($str) {
		if ($this->verbose === true) {
			echo "[VERBOSE] ";
			$trace = debug_backtrace();
			$caller = $trace[1];
			if (isset($caller["class"])) {
				echo $caller["class"] . "->";
			}
			if (isset($caller["function"])) {
				echo $caller["function"] . "()";
			}	
			echo " ($str)\n";
		}
	}
	
	/**
   * Compiles data related to an object version ID 
   *
   * @param string $objectVersionID version ID
   * @return mixed: results
   */
	private function __objectStat($objectVersionID) {
		$this->notify("Fetching objectVersionID ($objectVersionID) from (" . $this->sdbDomainVersionList . ")");
		if (is_null($objectVersionID)) {
			return null;
		}
		return $this->sdb->itemGet($this->sdbDomainVersionList, $objectVersionID);
	}
	
	public function getBucketObjects($prefix = null) {
		$this->sdb->verbose = $this->verbose;
		$this->notify("Start up with prefix ($prefix)");
		$objects = null;
		
		if (!is_null($prefix)) {
			$query = "SELECT * FROM `" . $this->sdbDomainMasterList . "` WHERE itemName() LIKE '$prefix%'";
			$this->notify("Executing query ($query)");
			$masterList = $this->sdb->domainSelect($query);
		} else {
			$this->notify("Grabbing everything in the domain");
			$masterList = $this->sdb->domainGetAll($this->sdbDomainMasterList);
		}
		$this->notify("Finished fetching");
		
		if (is_null($masterList)) {
			$this->notify("Got nothing back... returning empty array");
			return array();
		}
		
		$this->notify("Processing results");
		foreach ($masterList as $object) {
			$objectName = $object["uuid"];
			$this->notify("\t$objectName");
			$latestVersionID = $object["latestVersionID"];
			$versionCount = $object["versionCount"];
			
			if (trim($latestVersionID) == "") {
				$latestVersionID = null;
			}

			if (!is_null($latestVersionID)) { // Meaning, the file has been deleted, but versions still exist
				$itemDeleted = false;
				$objectStat = $this->__objectStat($latestVersionID);
				$size = Utils::getHumanSize($objectStat["size"]);
				$mtime = date("c", $objectStat["LastModified"]);
				$itemMD5 = $this->sdb->itemGet($this->sdbDomainVersionList, $latestVersionID, "md5");
				if (isset($itemMD5["md5"])) {
					$objects[$objectName]["md5"] = $itemMD5["md5"];
				}
			} else {
				$this->notify("\t\tObject was deleted!");
				$size = null;
				$mtime = null;
				$itemMD5 = null;
				$itemDeleted = true;
			}
				
			//$count = null;
			//$countObjects = $this->sdb->domainSelect("SELECT count(*) FROM `" . $this->sdbDomainVersionList . "` WHERE name = '$objectName'");
			//if (!is_null($countObjects)) {
			//	$count = $countObjects["Domain"]["Count"];
			//}
			
			$objects[$objectName]["deleted"] = $itemDeleted;
			$objects[$objectName]["size"] = $size;
			$objects[$objectName]["mtime"] = $mtime;
			$objects[$objectName]["versionCount"] = $versionCount;
			$objects[$objectName]["latestVersionID"] = $latestVersionID;
		}
		$this->notify("Results processed, returning object");
		return $objects;
	}
}
?>